package cn.edu.xmist;


import java.util.*;

public class StudentHashMap {
    static {
        System.out.println("类进行加载时执行");
    }
    /**
     * 用来保存学生对象
     */
   public static Map<Integer,Student> studentMap=new HashMap<>();
    /**
     * 新增学生
     * @param student
     * @return
     */
    public static boolean addStudent(Student student) {
        if (studentMap.containsKey(student.getStuNo())) {
            return Boolean.FALSE;
        } else {
         studentMap.put(student.getStuNo(),student);
         return Boolean.TRUE;
        }
    }

    public static Map<Integer,Student> querallStudent() {
        return studentMap;
    }



    /**
     * 根据学号(唯一性)删除集合中指定的学生对象
     * @param stuNo
     * @return
     */
    public static boolean delStudentById(int stuNo) {
        int index = -1;
        if (studentMap.containsKey(stuNo)) {
            studentMap.remove(stuNo);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

      /** for (int i = 0; i < studentList.size(); i++) {
            Student student = studentList.get(i);
            if(stuNo== student.getStuNo()){
                index = i;
                break;
            }
        }
        if (index != -1) {
            studentList.remove(index);
            return true;
        } else {
            return false;
        }
    }
    */

public  static  boolean updStudentById(Student student){
    if (studentMap.containsKey(student.getStuNo())) {
        studentMap.put(student.getStuNo(),student);
        return Boolean.TRUE;
    }
        return Boolean.FALSE;

}

    /**
     * 应用程序入口方法
     * @param args
     */
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        boolean flag=true;
        do {
            System.out.println("===学生信息管理系统===");
            System.out.println("1.增加学生信息");
            System.out.println("2.修改学生信息");
            System.out.println("3.删除学生信息");
            System.out.println("4.显示所有学生");
            System.out.println("0.退出");
            System.out.println("请选择：");
            int choice=input.nextInt();
            switch (choice){
                case 1:
                    System.out.println("请输入学号：");
                    int stuNo=input.nextInt();
                    System.out.println("请输入姓名：");
                    String name=input.next();
                    System.out.println("请输入年龄：");
                    int age=input.nextInt();
                    System.out.println("请输入身份证号：");
                    String idNumber=input.next();
                    Student student=new Student(stuNo,age,name,idNumber);
                    boolean result=addStudent(student);
                    if(result){
                        System.out.println("新增学生信息成功！");
                    }else {
                        System.out.println("新增学生信息失败！");
                    }
                    break;
                case 2:
                    System.out.println("请输入学号:");
                    stuNo= input.nextInt();
                    System.out.println("请输入姓名:");
                    name = input.next();
                    System.out.println("请输入年龄:");
                    age = input.nextInt();
                    System.out.println("请输入身份证号:");
                    idNumber = input.next();
                    Student student1=new Student(stuNo,age,name,idNumber);
                    updStudentById(student1);
                    System.out.println("修改成功");
                    break;
                case 3:
                    System.out.println("请输入学号：");
                    stuNo= input.nextInt();
                    delStudentById(stuNo);
                    System.out.println("删除成功");
                    break;
                case 4:
                    Map<Integer, Student> allstudent = querallStudent();
                    Set<Integer> iterators = allstudent.keySet();
                    System.out.println("学号\t\t姓名\t\t年龄\t\t身份证号");
                    System.out.println("---------------------------------------- -");

                    for (Integer no : iterators) {
                        Student stu = studentMap.get(no);
                        System.out.println(stu.getStuNo() + "\t" + stu.getName()
                                + "\t\t" + stu.getAge() + "\t\t" + stu.getIdNumber());
                        System.out.println("-----------------------------------------");
                    }

                    break;
                case 0:

                    System.out.println("谢谢使用本系统，欢迎下次光临！");
                    flag = false;
                    break;
                default:
                    System.out.println("你输入的序号无效，请重新输入");
                    flag = true;
                    break;
            }
        }while (flag);
    }
}

